package com.xinmiao.test8;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AnticipateInterpolator;

/**
 * Created by ${王俊强} on 2017/11/16.
 */

public class YuanView2 extends View {
    private ValueAnimator animator;
    private boolean istrue = true;

    public YuanView2(Context context) {
        super(context);
        init();
    }


    public YuanView2(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public YuanView2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private Paint mPaint, mPaint1;
    private int mIntRate1, mIntRate;

    private void init() {
        mPaint = new Paint();
        mPaint1 = new Paint();
        mPaint.setAntiAlias(true);
        mPaint1.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint1.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(3);
        mPaint.setColor(Color.parseColor("#ff0000"));
        mPaint1.setColor(Color.parseColor("#ffffff"));
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int w = getWidth() - getPaddingLeft() - getPaddingEnd();
        int y = getHeight() - getPaddingTop() - getPaddingBottom();

        int min = Math.min(w, y);
        mIntRate = min / 2 - 10;
        canvas.drawCircle(getPaddingLeft()+w/2,
                getPaddingTop()+y/2, mIntRate, mPaint);
        if (mIntRate1 == 0) {
            mIntRate1 = mIntRate - 3;
        }

        canvas.drawCircle(getPaddingLeft()+w/2,
                getPaddingTop()+y/2, mIntRate1, mPaint1);

    }

    public void clearyuan(int time) {

        if (!istrue) {
            return;
        }
        if (animator == null) {
            animator = ObjectAnimator.ofInt(mIntRate1, 10);
            animator.setInterpolator(new AnticipateInterpolator());
            animator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationStart(Animator animation) {
                    super.onAnimationStart(animation);
                    istrue = false;
                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    istrue = true;
                    postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mIntRate1 = mIntRate - 10;
                            postInvalidateOnAnimation();
                        }
                    }, 1500);
                }
            });
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    mIntRate1 = (int) valueAnimator.getAnimatedValue();
                    mIntRate = mIntRate1;
                    int w = getWidth() - getPaddingLeft() - getPaddingEnd();
                    int y = getHeight() - getPaddingTop() - getPaddingBottom();

                    if (mIntRate < Math.min(w, y)/2 - 10) {
                        mIntRate = Math.min(w, y)/2 - 10;
                    }
                    postInvalidateOnAnimation();
                }

            });
        }


        animator.setDuration(time > 200 ? time : 200);
        animator.start();
    }
}
